<?php
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller{
    //分配登录模板
    public function index(){
        $this->check_yitina=cookie("check_yitian");
        $this->pass=session("pass");
        $this->display("login");
    }
    //接收数据
    public function loginUser(){
        //1.判断数据来源
        if(IS_POST){
            $post=I('post.');
            $lasttime['lasttime']=time();
            $lasttime['lastip']=get_client_ip();
            if(empty($post['username'])||empty($post['password'])) {
                $this->error("ئىسمى ھەم پارول بوش قالمىسۇن!", "", 2);
                exit;
            }
            $row=M("login")->where(array("username"=>$post["username"],"status"=>1))->find();
            if(!$row){
                $this->error("بۇ ئابونت ئىسمى مەۋجۇت ئەمەس ياكى قوزغىتىلمىغان! ","",2);exit;
            }
            if (sha1($post['password'])!=$row['password']) {
                $this->error(" پارول خاتا !", "", 1);
                exit;
            }if(1!=$this->check_verify($post['verify'])){
                $this->error("تەستىق كودى خاتا!","",1);exit;
            }
            else{
                //记住一天  //agar tallanhan bolsa bir kun asta sahlaydu
                if(isset($_POST['check'])){
                    cookie("check_yitian","一天",86400);
                    session("pass",$_POST['password']);
                }
                session("username",$row['username']);
                //sestimiha kirgan ketim sani
                $cishu=M('login')->where(array("username"=>$post["username"]))->find();
                $i=$cishu['cishu'];
                //sestimiha kirgan wakitni hatirlax
                session("lasttime",time());
                session($post["username"]."cishu",$i+1);
                M("login")->where(array("username"=>$post["username"]))->save($lasttime);
                M('login')->where(array("username"=>$post["username"]))->setInc('cishu',1);
                //记录登陆的人
                $data=array(
                    "username"=>$post['username'],
                    "time"=>date("Y/m/d H:i:s",time()),
                    "ip"=>get_client_ip()
                    );
                M('login_om')->data($data)->add();
                $this->success("كىرىش مۇۋاپىقىيەتلىك بولدى",U("Index/index"),2);
            }
        }else{
            $this->error("مەشۇلات خاتا!",U("Login/index"));
            exit;
        }
    }
    public function selfverify(){
        $config =    array(
            'useCurve'    =>   false ,
            'useImgBg'    =>   false ,
            'fontSize'    =>    17,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'expire'      =>     15 ,
            'imageW'      =>      125,
            'imageH'      =>  34,
            'bg'          => array(243, 251, 254)
        );
        $verify = new \Think\Verify($config);
        $verify->codeSet = '0123456789';
        $verify->entry();
    }
    function check_verify($code,$id=''){
        $verify = new \Think\Verify();
        return $verify->check($code,$id);
    }
}